/**
 *====================================================
 * 文件名称: MemberGradeServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.member.entity.MemberGradeEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: MemberGradeServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:55:49
 */
@Implements(MemberGradeService.class)
public class MemberGradeServiceImpl implements MemberGradeService {

	private void validate(MemberGradeEntity entity) {
		DetachedCriteria dc = DetachedCriteria.forClass(MemberGradeEntity.class);
		dc.add(Restrictions.eq("gradeName", entity.getGradeName()));
		dc.add(Restrictions.eq("enabled", true));
		List<MemberGradeEntity> list = PersistUtil.get(dc);
		if (!list.isEmpty()) {
			throw new BusinessException("会员等级名称重复");
		}
	}

	@Override
	public Long createMemberGrade(MemberGradeEntity entity) {
		validate(entity);
		entity.setEnabled(true);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getMemberGradeId();
	}

	@Override
	@Transactional
	public void updateMemberGrade(MemberGradeEntity entity) {
		MemberGradeEntity e = inquireMemberGradeDetail(entity.getMemberGradeId());
		if (!e.getGradeName().equals(entity.getGradeName())) {
			validate(entity);
		}
		e.setGradeName(entity.getGradeName());
		e.setInterestRate(entity.getInterestRate());
		e.setPenalInterest(entity.getPenalInterest());
		e.setCreditLine(entity.getCreditLine());
		e.setRemark(entity.getRemark());
		e.setExpression(entity.getExpression());
		e.setMaxNumerica(entity.getMaxNumerica());
		e.setMinNumerica(entity.getMinNumerica());
		e.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(e);
	}

	@Override
	@Transactional
	public void disableMemberGrade(Long memberGradeId) {
		MemberGradeEntity e = inquireMemberGradeDetail(memberGradeId);
		e.setEnabled(false);
		e.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(e);
	}

	@Override
	public MemberGradeEntity inquireMemberGradeDetail(Long memberGradeId) {
		MemberGradeEntity e = PersistUtil.get(MemberGradeEntity.class, memberGradeId);
		if (e == null || !e.getEnabled()) {
			throw new BusinessException("该会员等级不存在");
		}
		return e;
	}

	@Override
	public MemberGradeEntity inquireMemberGrade(Long memberGradeId) {
		return PersistUtil.get(MemberGradeEntity.class, memberGradeId);
	}

	@Override
	public PageList<MemberGradeEntity> inquireMemberGradeList(PageCriteria page) {
		DetachedCriteria dc = DetachedCriteria.forClass(MemberGradeEntity.class);
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		PageList<MemberGradeEntity> memberGradeEntityPageList = PersistUtil.get(dc, page);
		return memberGradeEntityPageList;
	}

	@Override
	public PageList<MemberGradeEntity> inquireMemberGradeByGradeName(PageCriteria page, String gradeName) {

		DetachedCriteria dc = DetachedCriteria.forClass(MemberGradeEntity.class);
		if (StringUtil.isNotBlank(gradeName)) {
			dc.add(Restrictions.eq("gradeName", gradeName));
		}
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		PageList<MemberGradeEntity> memberGradeEntityPageList = PersistUtil.get(dc, page);
		return memberGradeEntityPageList;
	}
}
